<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>foreach</title>
</head>

<body>

    <script>
        // foreach----------------------------------------------------
        Array.prototype.forEachCustom = function (fn, thisV) {
            try {
                for (let i in this) {
                    fn(this[i], i, this); //调试时候下标出现过问题(for in)
                    //    fn.call(thisV,this[i],i,this)  thisV??????
                }
            } catch (e) {
                if (e === forEachCustom.break) return;
                else throw e;
            }
        }
        let arrf = [22, 33, 44, 55]
        let arrf2 = arrf.forEachCustom(function (val, i) {
            console.log("下标" + i + ":" + val);
        })



        // 作业修改--------------------------------------
        //深拷贝-----------------------------------------------------
        // 备注： JSON.parse(JSON.stringify(obj1));  //无效function
        function deepClone(data) {
            var type = typeof data;
            var obj;
            if (type === 'array') {
                obj = [];
            } else if (type === 'object') {
                obj = {};
            } else {
                return data;
            }
            if (type === 'array') {
                for (let i = 0; i < data.length; i++) {
                    obj.push(data[i]);
                }
            } else if (type === 'object') {
                obj = JSON.parse(JSON.stringify(data));
            }
            return obj;
        }
        let arr = [2, 3, 4, 5];
        let arr2 = deepClone(arr);
        console.log(arr2); //[2,3,4,5]
        arr2[0] = 1000;
        console.log(arr); //[2,3,4,5]
        console.log(arr2); //[1000, 3, 4, 5]

        function a() {
            return 23;
        }
        // function b(){};
        let b = deepClone(a);
        console.log(b);
    </script>
</body>

</html>